home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / grayimage / image_rect.cc < prev    next >
Encoding:
Text File  |  1994-06-30  |  5.2 KB  |  201 lines  |  [TEXT/R*ch]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *         Implementation of the Primitive Operations
  7.  *             on the rectangular area of the image
  8.  *
  9.  *   The image is represented as a Pixmap, i.e. a matrix of pixels
  10.  *    each of them specifies the gray level at a particular point
  11.  *
  12.  ************************************************************************
  13.  */
  14.  
  15. #include "image.h"
  16.  
  17. #pragma implementation
  18.  
  19.  
  20. /*
  21.  *------------------------------------------------------------------------
  22.  *    Construct a new image from the rectangular area of another image
  23.  */
  24.  
  25. IMAGE::IMAGE(const Rectangle& ra)
  26. {
  27.   allocate(ra.nrows,ra.ncols,ra.image.bits_per_pixel);
  28.   register GRAY * ps = ra.ptr;
  29.   register GRAY * pi = pixels;
  30.   register int i,j;
  31.  
  32.   for(i=0; i < ra.nrows; i++, ps += ra.inc_to_nextrow)
  33.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  34.       *pi++ = *ps++;
  35.  
  36.   assert( pi == pixels + npixels );
  37. }
  38.  
  39. /*
  40.  *------------------------------------------------------------------------
  41.  *        Modify all the pixels in the rectangular area
  42.  *          according to a particular operation
  43.  */
  44.  
  45.                 // Assign a value to all the pixels
  46. Rectangle& Rectangle::operator = (const int val)
  47. {
  48.   register GRAY * pp = ptr;
  49.   register int i,j;
  50.  
  51.   for(i=0; i < nrows; i++, pp += inc_to_nextrow)
  52.     for(j=0; j < ncols; j++)        // Proceed scanline by scanline
  53.       *pp++ = val;
  54.  
  55.   return *this;
  56. }
  57.  
  58.                 // Increment the pixel value over the
  59.                 // rectangular area
  60. void operator += (const Rectangle& ra, const int val)
  61. {
  62.   register GRAY * pp = ra.ptr;
  63.   register int i,j;
  64.  
  65.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  66.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  67.       *pp++ += val;
  68. }
  69.  
  70.                 // Decrement the pixel value over the
  71.                 // rectangular area
  72. void operator -= (const Rectangle& ra, const int val)
  73. {
  74.   register GRAY * pp = ra.ptr;
  75.   register int i,j;
  76.  
  77.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  78.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  79.       *pp++ -= val;
  80. }
  81.  
  82.                 // Multiply the pixel value over the
  83.                 // rectangular area by the const
  84. void operator *= (const Rectangle& ra, const int val)
  85. {
  86.   register GRAY * pp = ra.ptr;
  87.   register int i,j;
  88.  
  89.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  90.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  91.       *pp++ *= val;
  92. }
  93.  
  94.                 // Perform the logical OR on the pixel values 
  95.                 // over the rectangular area
  96. void operator |= (const Rectangle& ra, const int val)
  97. {
  98.   register GRAY * pp = ra.ptr;
  99.   register int i,j;
  100.  
  101.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  102.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  103.       *pp++ |= val;
  104. }
  105.  
  106.                 // Perform the logical AND on the pixel values 
  107.                 // over the rectangular area
  108. void operator &= (const Rectangle& ra, const int val)
  109. {
  110.   register GRAY * pp = ra.ptr;
  111.   register int i,j;
  112.  
  113.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  114.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  115.       *pp++ &= val;
  116. }
  117.  
  118.                 // Perform the logical XOR on the pixel values 
  119.                 // over the rectangular area
  120. void operator ^= (const Rectangle& ra, const int val)
  121. {
  122.   register GRAY * pp = ra.ptr;
  123.   register int i,j;
  124.  
  125.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  126.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  127.       *pp++ ^= val;
  128. }
  129.  
  130.                 // Shift the pixel values 
  131.                 // over the rectangular area to the left
  132. void operator <<= (const Rectangle& ra, const int val)
  133. {
  134.   register GRAY * pp = ra.ptr;
  135.   register int i,j;
  136.  
  137.   if( abs(val) >= GRAY_MAXBIT )
  138.     _error("Very fishy shift factor: %d",val);
  139.  
  140.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  141.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  142.       *pp++ <<= val;
  143. }
  144.  
  145.                 // Shift the pixel values 
  146.                 // over the rectangular area to the right
  147. void operator >>= (const Rectangle& ra, const int val)
  148. {
  149.   register GRAY * pp = ra.ptr;
  150.   register int i,j;
  151.  
  152.   if( abs(val) >= GRAY_MAXBIT )
  153.     _error("Very fishy shift factor: %d",val);
  154.  
  155.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  156.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  157.       *pp++ >>= val;
  158. }
  159.  
  160.  
  161.                     // Get a total sum of all the pixels
  162.                     // over the rectangular area
  163. double sum_over(const Rectangle& ra)
  164. {
  165.   register GRAY_SIGNED * pp = (GRAY_SIGNED *)ra.ptr;
  166.   register double sum = 0;
  167.   register int i,j;
  168.  
  169.   for(i=0; i < ra.nrows; i++, pp += ra.inc_to_nextrow)
  170.     for(j=0; j < ra.ncols; j++)        // Proceed scanline by scanline
  171.       sum += *pp++;
  172.  
  173.   return sum;
  174. }
  175.  
  176. /*
  177.  *------------------------------------------------------------------------
  178.  *        Operations on the rectangle area as a whole
  179.  */
  180.  
  181.                 // Copy a rectangle a_rect into the
  182.                 // rectangular area of the image
  183. Rectangle& Rectangle::operator = (const Rectangle& a_rect)
  184. {
  185.  
  186.   if( a_rect.nrows != nrows || a_rect.ncols != ncols )
  187.     _error("Rectangles %dx%d and %dx%d are incompatible",
  188.        a_rect.nrows,a_rect.ncols,nrows,ncols);
  189.  
  190.   register GRAY * sp = a_rect.ptr;        // Source and
  191.   register GRAY * dp = ptr;            // destination ptrs
  192.   register int i;
  193.  
  194.   for(i=0; i < nrows; i++)
  195.     memcpy(dp,sp,ncols*sizeof(GRAY)),
  196.     sp += a_rect.inc_to_nextrow + ncols, 
  197.     dp += inc_to_nextrow + ncols;
  198.  
  199.   return *this;
  200. }
  201.